package cn.xinfei.xdecision.manager.controller.component.folder;

import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.component.folder.Folder;
import cn.xinfei.xdecision.common.model.component.folder.vo.FolderDeleteReqVo;
import cn.xinfei.xdecision.common.model.component.folder.vo.FolderListReqVo;
import cn.xinfei.xdecision.common.model.component.folder.vo.FolderRenameReqVo;
import cn.xinfei.xdecision.common.model.component.folder.vo.FolderSaveReqVo;
import cn.xinfei.xdecision.common.model.login.SessionManager;
import cn.xinfei.xdecision.common.service.component.folder.FolderService;
import cn.xinfei.xdecision.common.utils.constant.OperateNameConst;
import cn.xinfei.xdecision.common.utils.constant.OperateTypeEnum;
import cn.xinfei.xdecision.manager.web.aop.ArchivesLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by maying on 2023/03/21.
 **/
@Api(tags = "通用-文件夹")
@RequestMapping("/v3/folder")
@RestController("folderControllerV3")
public class FolderController {
	@Autowired
	private FolderService folderService;
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ApiOperation(value = "查询文件夹列表", notes = "根据组件类型(componentType)查询组件文件夹列表")
	public ResponseEntityDto<Object> list(@RequestBody @Valid FolderListReqVo folderListReqVo) {
		List<Folder> list = folderService.getTreeList(folderListReqVo);
		return ResponseEntityBuilder.buildNormalResponse(list);
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ApiOperation(value = "新增文件夹", notes = "新增文件夹")
	@ArchivesLog(type = OperateTypeEnum.CREATE,name = OperateNameConst.FolderOpt.SAVE)
	public ResponseEntityDto<Object> save(@RequestBody @Valid FolderSaveReqVo folderSaveReqVo) {
		return ResponseEntityBuilder.buildNormalResponse(folderService.addFolder(folderSaveReqVo));
	}

	@RequestMapping(value = "/rename", method = RequestMethod.POST)
	@ApiOperation(value = "重命名文件夹", notes = "重命名文件夹")
	@ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.FolderOpt.RENAME)
	public ResponseEntityDto<Object> renameFolder(@RequestBody @Valid FolderRenameReqVo folderRenameReqVo){
		return ResponseEntityBuilder.buildNormalResponse(folderService.renameFolder(folderRenameReqVo));
	}

	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除文件夹", notes = "删除文件夹及其子文件夹，并删除文件夹下所有组件")
	@ArchivesLog(type = OperateTypeEnum.DELETE,name = OperateNameConst.FolderOpt.DELETE)
	public ResponseEntityDto<Object> deleteFolder(@RequestBody @Valid FolderDeleteReqVo folderDeleteReqVo){
		Long userId = SessionManager.getLoginAccount().getId();
		folderDeleteReqVo.setUserId(userId);
		return ResponseEntityBuilder.buildNormalResponse(folderService.deleteFolder(folderDeleteReqVo));
	}
}
